package com.yunxi.service.jpush;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.Notification;
import org.springframework.stereotype.Component;

import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.swing.*;
import java.awt.print.Book;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;

/**
 * Created by xuruzi on 2017/2/22.
 */
@Component
public class JpushServiceImpl {
    public static String appKey = "03e6e91d3eda866159962343";
    public static String masterSecret = "024a9e6b697b28b4fa824ca6";

//    public static JpushService getInstance(){
//        return new JpushService();
//    }

    public static int send(String userCode,String roleCode,String content ){
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());

        // For push, all you need do is to build PushPayload object.
        //PushPayload payload = buildPushObject_all_alias_tag_alert(userCode,roleCode,content);
        //PushPayload payload = buildPushObject_all_all_alert();
        //PushPayload payload = buildPushObject_all_alias_alert(userCode,content);
        PushPayload payload = buildPushObject_all_tag_alert(roleCode,content);
        try {
            PushResult result = jpushClient.sendPush(payload);
            return result.getResponseCode();
        } catch (APIConnectionException e) {
            // Connection error, should retry later

        } catch (APIRequestException e) {

        }
        return 1000;
    }

    public static int send(String userCode,String content){
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());

        // For push, all you need do is to build PushPayload object.
        PushPayload payload = buildPushObject_all_alias_alert(userCode,content);

        try {
            PushResult result = jpushClient.sendPush(payload);
            return result.getResponseCode();
        } catch (APIConnectionException e) {
            // Connection error, should retry later

        } catch (APIRequestException e) {

        }
        return 1000;
    }

    public static PushPayload buildPushObject_all_all_alert() {
        return PushPayload.alertAll("哎哟喂");
    }

    public static PushPayload buildPushObject_all_alias_alert(String userCode,String content) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(userCode))
                .setNotification(Notification.alert(content))
                .build();
    }

    public static PushPayload buildPushObject_all_tag_alert(String userCode,String content) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.tag(userCode))
                .setNotification(Notification.alert(content))
         //       .setMessage(Message.newBuilder()
           //             .setMsgContent(content)
             //           .addExtra("from", "JPush")
               //         .build())
                .build();
    }

    public static PushPayload buildPushObject_all_alias_tag_alert(String userCode, String roleCode, String content) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag(roleCode))
                        .addAudienceTarget(AudienceTarget.alias(userCode))
                        .build())
                .setNotification(Notification.alert(content))
                .build();
    }

    public static void main(String args[]){
        JpushServiceImpl js = new JpushServiceImpl();
        /*
        Map content = new HashMap();
        content.put("title","订单审核");
        content.put("content","您的订单已审核通过,请完成支付!");
        content.put("type",6 );//跳转信息
        js.sendMessage(content,"admin0001");
        */
        //JpushServiceImpl.send("SHYP001","rolePartnerSale","我去啊 角色 与 用户绑定 传的");
        JpushServiceImpl.printCnzz();
    }

    public static boolean printCnzz() {
        // 创建打印作业
       // JFileChooser fileChooser = new JFileChooser();

      //  int state = fileChooser.showOpenDialog(null);
      //  if (state == fileChooser.APPROVE_OPTION) {
            // 获取选择的文件
            File file = new File("D:/msg.txt");
            // 构建打印请求属性集
            HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            // 设置打印格式,因为未确定类型,所以选择autosense
            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
            // 查找所有的可用的打印服务
            PrintService printService[] = PrintServiceLookup
                    .lookupPrintServices(flavor, pras);
            // 定位默认的打印服务
            PrintService defaultService = PrintServiceLookup
                    .lookupDefaultPrintService();
            // 显示打印对话框
            //PrintService service = ServiceUI.printDialog(null, 200, 200,
            //        printService, defaultService, flavor, pras);
            //if (service != null) {
                try {
                    DocPrintJob job = defaultService.createPrintJob();
                    // 创建打印作业
                    FileInputStream fis = new FileInputStream(file);
                    // 构造待打印的文件流
                    DocAttributeSet das = new HashDocAttributeSet();
                    Doc doc = new SimpleDoc(fis, flavor, das);
                    job.print(doc, pras);
                } catch (Exception e) {
                    e.printStackTrace();
                }
           // }
     //   }
        return true;
    }

    public static void print(){

        Book book = new Book();
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(book);
    }
}
